package yjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yjh.mapper.MistakesTopicMapper;
import yjh.mapper.TopicTableMapper;
import yjh.pojo.MistakesTopic;
import yjh.pojo.TopicTable;

import java.util.List;
import java.util.Random;

@Service
public class TopicTableServiceImpl implements TopicTableService {
    @Autowired
    public TopicTableMapper topicTableMapper;
    @Autowired
    public MistakesTopicMapper mistakesTopicMapper;

    /**
     * 添加题目
     * @param id 如果为null，使用主键自增长
     * @param topic
     * @param answer
     * @param importanc
     * @return
     */
    public String addTopic(Integer id,String topic,
                            String answer,Integer importanc ){

        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(topic);
        if (topicTable != null) {
            return "题目已存在";
        }

        boolean addStatus = topicTableMapper.insertTopicTable(id, topic, answer, importanc);
        if (!addStatus) {
            return "题目增加失败";
        }

        return null;
    }

    /**
     * 删除题目
     * @param topic
     * @return
     */
    public String deleteTopic(String topic){
        if (topic == null) {
            return "topic为null";
        }
        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(topic);

        if (topicTable==null) return "题目不存在";

        Integer tid = topicTable.getId();
        Integer mid = topicTable.getMid();

        if (tid==null) return "数据错误";


        if (mid != null) {
            System.out.println("mid ----- " + mid);

            //错题存在
            boolean deleted = mistakesTopicMapper.deleteMistakesTopicByid(mid);

            if (!deleted) return "删除失败";
        }

        boolean deletedStatus = topicTableMapper.deleteTopicTable(topic);
        if (!deletedStatus) return "删除题目失败";
        return null;
    }


    /**
     * 更新修改
     * 为null的参数不予修改
     * @param afterChangeTopic
     * @param answer
     * @param importanc
     * @param beforeChangeTopic
     * @return
     */
    public String updateTopic( String afterChangeTopic, String answer, Integer importanc,String beforeChangeTopic){
        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(beforeChangeTopic);

        if (topicTable == null||topicTable.getTopic()==null) {
            return "数据不存在";
        }

        boolean updateStatus = topicTableMapper.updateTopicTable(afterChangeTopic, answer, importanc, beforeChangeTopic);

        if (!updateStatus) return "更新失败";
        return null;
    }

    /**
     * 查询全部录入的题目、答案、重要度
     * @return
     */
    public List<TopicTable> findAllTopic(){
        return topicTableMapper.selectTopicTableAll();
    }

    /**
     * 根据题目查询相关信息
     * @param topic
     * @return
     */
    public TopicTable findOneByTopic(String topic){

        return topicTableMapper.selectTopicTableOneByTopic(topic);
    }


    /**
     * 随机查询
     * @return
     */
    public TopicTable findOneTopicByRandom(int selectType ,String data){

        System.out.println(data);
//        System.out.println(Integer.valueOf(data));

        List<TopicTable> topicTables = null;
        int count = 0;

        if (selectType==1) {
            //全题库
            topicTables = topicTableMapper.selectTopicTableAll();
            //题目个数
            count = topicTableMapper.selectTopicTableCount();
        }
        else if (selectType==2&&data!=null){
            Integer d = Integer.valueOf(data);

            System.out.println(d);

            if (d<=0||d>4) return null;
            //重要度
            topicTables = topicTableMapper.selectTopicTableByImportanc(d);
            System.out.println(topicTables);
            //题目个数
            count = topicTableMapper.selectTopicTableCountByImportanc(d);

            System.out.println(count);
        }

        if (count==0) return null;

        TopicTable topicTable;
        do {
            //生成对应范围的随机数
            Random r = new Random();
            Integer randomID = r.nextInt(count);

            topicTable = topicTables.get(randomID);

        }while (topicTable==null);

        return topicTable;
    }

    public List<TopicTable> findAllByImportanc(int importanc){
        return topicTableMapper.selectTopicTableByImportanc(importanc);
    }

    public String addMid(String topic){
        System.out.println("topic" + topic);
        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(topic);

        System.out.println("topic :"+ topic);

        if (topicTable == null) return "题目不存在";

        Integer tid = topicTable.getId();
        MistakesTopic mistakesTopic = mistakesTopicMapper.selectMistakesTopicByTopicID(tid);

//        System.out.println("mid: " + mid);

        Integer mid = mistakesTopic.getId();

        topicTableMapper.updateMid(mid,tid);

        return null;
    }
}
